package com.study.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.model.pojo.Emp;
import com.study.model.pojo.Empwithright;
import com.study.model.pojo.Menubar;
import com.study.model.pojo.Rights;
import com.study.services.EmpServices;
import com.study.services.EmpwithrightServices;
import com.study.services.MenubarServices;
import com.study.services.RightsServices;
import com.study.services.TodoServices;

@RestController
@CrossOrigin
public class RightsController {
	@Autowired
	RightsServices rs;
	@Autowired
	MenubarServices ms;
	@Autowired
	EmpwithrightServices ers;
	@Autowired
	EmpServices es;
	@Autowired
	TodoServices tds;
	
	
	@RequestMapping("/showright")
	public JSONObject showright(int deptno) {
		System.out.println("进入了showright");
		JSONObject json = new JSONObject();
		JSONArray json1 = new JSONArray();
		JSONArray json2 = new JSONArray();
		List<Rights> list=rs.findAllrightBydt(deptno);
		List<Emp> list2=es.findNoright(deptno);
		for(Rights r:list){
			JSONObject jo = new JSONObject();
			jo.put("rid", r.getRid());
			jo.put("rname", r.getRname());
			jo.put("deptno", r.getDeptno());
			jo.put("num", ers.queryNumbyr(r.getRid()));
			json1.add(jo);			
		}
		for(Emp e:list2){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("pwd", e.getPwd());
			jo.put("sex", e.getSex());
			jo.put("tel", e.getTel());
			jo.put("adss", e.getAdss());
			jo.put("birth", e.getBirth());
			jo.put("job", e.getJob());
			jo.put("deptno", e.getDeptno());
			jo.put("salary", e.getSalary());
			System.out.println("入职日期"+e.getInductDate());
			jo.put("induct_date", e.getInductDate());
			jo.put("leaderid",e.getLeaderid());
			jo.put("depart_date", e.getDepartDate());
			jo.put("state", e.getState());
			json2.add(jo);			
		}
		json.put("rights",json1);
		json.put("emps",json2);
		System.out.print("rights:"+json);
		return json;
	}
	/**
	 * 增加权限*/
	@RequestMapping("/addright")
	public JSONObject addright(Rights r) {
		System.out.println("增加权限");
		rs.addRights(r);
		Menubar m=new Menubar();
		m.setMname(r.getRname());
		m.setRid(r.getRid());
		ms.addMenu(m);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	/**
	 * 给员工添加权限*/
	@RequestMapping("/empaddrt")
	public JSONObject empaddrt(int eid,int rid) {
		System.out.println("给员工添加权限");
		Empwithright er=new Empwithright(){};
		er.setEid(eid);
		er.setRid(rid);
		ers.addEWR(er);
		int leadid=es.findMng(eid);
		if(tds.queryTDmngandtype(leadid,22).getNum()>1){
			tds.redTodonum(tds.queryTDmngandtype(leadid,22).getToid());
		}else {
			tds.delTodo(tds.queryTDmngandtype(leadid,22).getToid());
		}
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
	
	/**
	 * 点击按钮,传入没有权限的员工*/
	@RequestMapping("/sempnoright")
	public JSONArray sempnoright(int deptno,int rid) {
		JSONArray json = new JSONArray();
		List<Emp> list2=es.findNothisright(deptno, rid);
		for(Emp e:list2){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("pwd", e.getPwd());
			jo.put("sex", e.getSex());
			jo.put("tel", e.getTel());
			jo.put("adss", e.getAdss());
			jo.put("birth", e.getBirth());
			jo.put("job", e.getJob());
			jo.put("deptno", e.getDeptno());
			jo.put("salary", e.getSalary());
			jo.put("induct_date", e.getInductDate());
			jo.put("leaderid",e.getLeaderid());
			jo.put("depart_date", e.getDepartDate());
			jo.put("state", e.getState());
			json.add(jo);			
		}
		return json;
	}
	/**
	 * 为员工增加权限*/
	@RequestMapping("/shright")
	public JSONArray shright(int deptno) {
		JSONArray json = new JSONArray();
		List<Rights> list=rs.findAllrightBydt(deptno);
		for(Rights r:list){
			JSONObject jo = new JSONObject();
			jo.put("rid", r.getRid());
			jo.put("rname", r.getRname());
			jo.put("deptno", r.getDeptno());
			jo.put("num", ers.queryNumbyr(r.getRid()));
			json.add(jo);			
		}
		return json;
	}
	
	/**
	 * 点击按钮,传入没有权限的员工*/
	@RequestMapping("/emphasright")
	public JSONArray emphasright(int deptno) {
		JSONArray json = new JSONArray();
		List<Emp> list2=es.findhasright(deptno);
		for(Emp e:list2){
			JSONObject jo = new JSONObject();
			jo.put("eid", e.getEid());
			jo.put("ename", e.getEname());
			jo.put("sex", e.getSex());
			jo.put("power", rs.rightname(deptno, e.getEid()));
			json.add(jo);					
		}
		return json;
	}
	
	/**
	 * 员工没有该权限的选择*/
	@RequestMapping("/rightempno")
	public JSONArray rightempno(int deptno,int eid) {
		JSONArray json = new JSONArray();
		List<Rights> list=rs.notthisright(deptno, eid);
		for(Rights r:list){
			JSONObject jo = new JSONObject();
			jo.put("rid", r.getRid());
			jo.put("rname", r.getRname());
			jo.put("deptno", r.getDeptno());
			
			json.add(jo);			
		}
		return json;
	}
	
	/**
	 * 员工修改权限*/
	@RequestMapping("/changeright")
	public JSONObject changeright(int eid,int rid,int deptno) {
		System.out.println("进入员工修改权限");
		Map m=new HashMap(){};
		m.put("eid",eid);
		m.put("rid",rid);
		m.put("deptno", deptno);
		ers.changeEWR(m);
		JSONObject json = new JSONObject();
		System.out.println("right:"+rs.rightname2(rid));
		json.put("power", rs.rightname2(rid));
		return json;
	}
	
	/**
	 *删除权限*/
	@RequestMapping("/delright")
	public JSONObject delright(int rid) {
		System.out.println("删除权限");
		rs.delrights(rid);
		ms.delMenubyrid(rid);
		JSONObject json = new JSONObject();
		json.put("ans", "y");
		return json;
	}
}
